    #include "_ftp.h"

    namespace idc{
        cftpclient::cftpclient()
        {
            m_ftpcoon=0;
            m_loginfailed=0;
            m_optionfailed=0;
            m_connetfailed=0;
            initdata();
        }

        cftpclient::~cftpclient()
        {
            loginout();
        }

        bool cftpclient::size(const string& remotefile)
        {
            if(m_ftpcoon==0)
                return false;
            if(FtpSize(remotefile.c_str(),&m_msize,FTPLIB_IMAGE,m_ftpcoon)==0)
            {
                return false;
            }
            return true;
        }

        void cftpclient::initdata()
        {
            m_mtime.clear();
        }

        bool cftpclient::login(const string& host,const string& username,const string& password,const int fmode)
        {
            if(m_ftpcoon!=0)
            {
                FtpQuit(m_ftpcoon);
                m_ftpcoon=0;
            }
            m_loginfailed=m_optionfailed=m_connetfailed=0;
            if(FtpConnect(host.c_str(),&m_ftpcoon)==0)  {m_connetfailed=1;return false;}
            if(FtpLogin(username.c_str(),password.c_str(),m_ftpcoon)==0) {m_loginfailed=1;return false;}
            if(FtpOptions(FTPLIB_CONNMODE,long(fmode),m_ftpcoon)==0)  {m_optionfailed=1;return false;}
            return true;
        }

        bool cftpclient::loginout()
        {
            if(m_ftpcoon==0)
            {
                return false;
            }
            FtpQuit(m_ftpcoon);
            m_ftpcoon=0;
            return true;
        }

        bool cftpclient::mtime(const string& filename)
        {
            if(m_ftpcoon==0)  return false;
            m_mtime.clear();
            string mtimestr;
            mtimestr.resize(14);
            if(FtpModDate(filename.c_str(),&mtimestr[0],14,m_ftpcoon)==0) return false;
            //将UTC时间转换为实际时间
            if(addtime(mtimestr,m_mtime,0,"yyyymmddhh24miss")==0) return false;
            return true;
        }

        bool cftpclient::chdir(const string& dirname)
        {
            if (m_ftpcoon==0)  return false;
            if(FtpChdir(dirname.c_str(),m_ftpcoon)==0) return false;
            return true;
        }

        bool cftpclient::mkdir(const string& dirname)
        {
            if (m_ftpcoon==0)  return false;
            if(FtpMkdir(dirname.c_str(),m_ftpcoon)==0) return false;
            return true;
        }

        bool cftpclient::rmdir(const string& dirname)
        {
            if (m_ftpcoon==0)  return false;
            if(FtpRmdir(dirname.c_str(),m_ftpcoon)==0) return false;
            return true;
        }

        bool cftpclient::nlst(const string& remotedir,const string& filenamelist)
        {
            if (m_ftpcoon==0)  return false;
            newdir(filenamelist.c_str());
            if(FtpNlst(filenamelist.c_str(),remotedir.c_str(),m_ftpcoon)==0) return false;
            return true;
        }

        bool cftpclient::get(const string &remotefilename, const string &localfilename, bool bcheckname)
        {
            if (m_ftpcoon==0) return false;
            newdir(localfilename);
            string tmpfile=localfilename+".tmp";
            //获取远程服务器文件的时间
            if(mtime(remotefilename)==false) return false;
            //下载文件到本地的临时文件中
            if(FtpGet(remotefilename.c_str(),tmpfile.c_str(),FTPLIB_IMAGE,m_ftpcoon)==0)
            {
                perror("get");
                return false;
            }
            //对比文件下载前和下载后的时间,如果不同，说明文件发生了改变，终止
            if(bcheckname)
            {
                string strtime=m_mtime;
                if(mtime(remotefilename)==false) return false;
                if(strtime!=m_mtime) return false;
            }
            //更新文件信息
            setmtime(remotefilename,m_mtime);
            //将临时文件重命名为正式文件
            if(rename(tmpfile.c_str(),localfilename.c_str())==0) return false;

            return true;
        }

        bool cftpclient::put(const string &localfilename, const string &remotefilename,const bool bcheckname)
        {
            if (m_ftpcoon==0) return false;
            newdir(remotefilename);
            string tmpfile=remotefilename+".tmp";
            string filetime1,filetime2;
            //获取本地文件的时间
            filemtime(localfilename,filetime1); 
            //上传文件到远程服务器的临时文件中
            if(FtpPut(localfilename.c_str(),tmpfile.c_str(),FTPLIB_IMAGE,m_ftpcoon)==0) 
            {
                return false;
            }
            //对比文件上传前和上传后的时间,如果不同，说明文件发生了改变，终止
            if(bcheckname)
            {
                if(filemtime(localfilename,filetime2)==false) return false;
                if(filetime2!=filetime1)
                {
                    deletefile(tmpfile);
                    return false;
                }
            }
            //将临时文件重命名为正式文件
            if(rename(tmpfile.c_str(),remotefilename.c_str())==0) 
            {
                return false;
            }
            if(bcheckname==true)
            {
                if(size(remotefilename)==0) return false;
                if(filesize(localfilename)!=m_msize)
                {
                    deletefile(remotefilename);
                    return false;
                }
            }
            return true;
        }

        bool cftpclient::deletefile(const string &remotefilename)
        {
            if (m_ftpcoon==0) return false;
            if(FtpDelete(remotefilename.c_str(),m_ftpcoon)==0) return false;
            return true;
        }

        bool cftpclient::rename(const string &oldfilename, const string &newfilename)
        {
            if (m_ftpcoon==0) return false;
            if(FtpRename(oldfilename.c_str(),newfilename.c_str(),m_ftpcoon)==0) return false;
            return true;
        }

        bool cftpclient::site(const string &command)
        {
            if (m_ftpcoon==0) return false;
            if(FtpSite(command.c_str(),m_ftpcoon)==0) return false;
            return true;
        }

        char* cftpclient::response()
        {
            if(m_ftpcoon==0) return nullptr;
            return FtpLastResponse(m_ftpcoon);
        }
    };